﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using GeneralDFS.BusinessLogical.Nodes;
using GeneralDFS.BusinessLogical.RemoteObject;
using GeneralDFS.DataEntity.Config;

namespace GeneralDFS.BusinessLogical.RemoteServers
{
     [Serializable]
    public class DataNodeRemoteServer
    {
        private IChannel _channel;

        public void InitServer(DataNodeConfig config,DataNode dataNode)
        {
            LogUtil.LogInfo("----------Starting init DataNodeRemote Server------------");
            _channel = new TcpServerChannel(config.ServerName+"-Server", config.RpcPort);
            LogUtil.LogInfo(String.Format("channel name:{0} rpcPort:{1}", "datanode", config.RpcPort));
            ChannelServices.RegisterChannel(_channel, false);
            var remoteObject =new DataNodeRemoteObject(dataNode);
            RemotingServices.Marshal(remoteObject, config.ServerName + "-Server");
            LogUtil.LogInfo("----------init DataNodeRemote Server Success------------");
        }

        public void StopServer()
        {
            LogUtil.LogInfo("----------Stoping init DataNodeRemote Server------------");
            ((TcpServerChannel)_channel).StopListening(null);
            ChannelServices.UnregisterChannel(_channel);
            LogUtil.LogInfo("----------Stop DataNodeRemote Server Success------------");
        }
    }
}